package com.example.chinawenddingexpo.controller;

import com.example.chinawenddingexpo.model.EventBooking;
import com.example.chinawenddingexpo.service.EventBookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/booking")
public class EventBookingController {
    
    @Autowired
    private EventBookingService eventBookingService;
    
    // 获取用户预约
    @GetMapping("/user/{userId}")
    public Map<String, Object> getUserBookings(@PathVariable Long userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<EventBooking> bookings = eventBookingService.getUserBookings(userId);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", bookings);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取用户预约失败");
        }
        return result;
    }
    
    // 获取活动的预约
    @GetMapping("/event/{eventId}")
    public Map<String, Object> getEventBookings(@PathVariable Long eventId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<EventBooking> bookings = eventBookingService.getEventBookings(eventId);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", bookings);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取活动预约失败");
        }
        return result;
    }
    
    // 预约活动
    @PostMapping
    public Map<String, Object> bookEvent(@RequestBody Map<String, Object> bookingData) {
        Map<String, Object> result = new HashMap<>();
        try {
            Long userId = Long.valueOf(bookingData.get("userId").toString());
            Long eventId = Long.valueOf(bookingData.get("eventId").toString());
            Integer participants = Integer.valueOf(bookingData.get("participants").toString());
            String notes = bookingData.get("notes") != null ? bookingData.get("notes").toString() : "";
            
            EventBooking booking = eventBookingService.bookEvent(userId, eventId, participants, notes);
            if (booking != null) {
                result.put("code", 200);
                result.put("msg", "预约成功");
                result.put("data", booking);
            } else {
                result.put("code", 400);
                result.put("msg", "预约失败，活动可能已满或已结束");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "预约活动失败");
        }
        return result;
    }
    
    // 取消预约
    @DeleteMapping("/{bookingId}")
    public Map<String, Object> cancelBooking(@PathVariable Long bookingId) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = eventBookingService.cancelBooking(bookingId);
            if (success) {
                result.put("code", 200);
                result.put("msg", "取消成功");
            } else {
                result.put("code", 400);
                result.put("msg", "取消失败，预约不存在或状态异常");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "取消预约失败");
        }
        return result;
    }
    
    // 签到
    @PostMapping("/checkin")
    public Map<String, Object> checkInBooking(@RequestBody Map<String, String> checkInData) {
        Map<String, Object> result = new HashMap<>();
        try {
            String bookingCode = checkInData.get("bookingCode");
            boolean success = eventBookingService.checkInBooking(bookingCode);
            if (success) {
                result.put("code", 200);
                result.put("msg", "签到成功");
            } else {
                result.put("code", 400);
                result.put("msg", "签到失败，预约码无效或状态异常");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "签到失败");
        }
        return result;
    }
    
    // 获取预约详情
    @GetMapping("/{bookingId}")
    public Map<String, Object> getBookingDetail(@PathVariable Long bookingId) {
        Map<String, Object> result = new HashMap<>();
        try {
            EventBooking booking = eventBookingService.getBookingById(bookingId);
            if (booking != null) {
                result.put("code", 200);
                result.put("msg", "success");
                result.put("data", booking);
            } else {
                result.put("code", 404);
                result.put("msg", "预约不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取预约详情失败");
        }
        return result;
    }
}